<template>
	<view>
		<u-popup :show="visible" @close="close" @open="open" mode="bottom" zIndex="99999999">
			<view class="head">
				<view class="" @click="close">取消</view>
				<view class="" style="color: rgb(60, 156, 255);" @click="confirm">确认</view>
			</view>
			<picker-view :indicator-style="indicatorStyle" :value="value" @change="bindChange" class="picker-view">
				<picker-view-column>
					<view class="item" v-for="(item,index) in years" :key="index">{{item}}年</view>
				</picker-view-column>
				<picker-view-column>
					<view class="item" v-for="(item,index) in months" :key="index">{{item}}月</view>
				</picker-view-column>
				<picker-view-column>
					<view class="item" v-for="(item,index) in days" :key="index">{{item}}日</view>
				</picker-view-column>
			</picker-view>
		</u-popup>
	</view>
</template>

<script>
	const date = new Date()
	const years = []
	const year = date.getFullYear()
	const months = []
	const month = date.getMonth() + 1
	const days = []
	const day = date.getDate()
	for (let i = 1990; i <= date.getFullYear(); i++) {
		years.push(i)
	}
	for (let i = 1; i <= 12; i++) {
		months.push(i)
	}
	for (let i = 1; i <= 31; i++) {
		days.push(i)
	}
	
	export default {
		data() {
			return {
				years,
				months,
				days,
				value: [9999, month - 1, day - 1],
				indicatorStyle: `height: 50px;`,
				indexs: []
			}
		},
		props: {
			visible: {
				type: Boolean,
				default: false
			}
		},
		methods: {
			bindChange(v) {
				this.indexs = v.detail.value
			},
			open() {
				this.$emit('open')
			},
			close() {
				this.$emit('close')
			},
			confirm() {
				let year = years[this.indexs[0]]
				let month = months[this.indexs[1]]
				let day = days[this.indexs[2]]
				this.value = [year,month-1,day-1]
				year = '' + year;
				month = month < 10 ? '0' + month : '' + month;
				day = day < 10 ? '0' + day : '' + day;
				let date =  year + '-' + month + '-' + day;
				this.$emit('confirm',{
					year,
					month,
					day,
					date
				})
			}
		}
	}
</script>

<style>
	.head {
		height: 100rpx;
		font-size: 30rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 20rpx;
		box-sizing: border-box;
	}
	
	.picker-view {
		width: 750rpx;
		height: 500rpx;
		margin-top: 20rpx;
	}
	
	.item {
		height: 50rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		text-align: center;
	}
</style>
